package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.utils.UserInteraction;

/**
 * Created by Administrator on 2016/1/13.
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button backButton;
    private TextView registerbtn;
    private EditText loginUserName;
    private EditText loginPassword;
    private Button loginBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

        setListener();
    }

    private void setListener() {
        backButton.setOnClickListener(this);
        registerbtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
    }

    private void initView() {
        backButton = (Button)findViewById(R.id.login_back);
        registerbtn = (TextView)findViewById(R.id.register_btn);
        loginUserName = (EditText)findViewById(R.id.login_user_name);
        loginPassword = (EditText)findViewById(R.id.login_user_password);
        loginBtn = (Button)findViewById(R.id.login_submit_btn);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_back:
                finish();
                break;
            case R.id.register_btn:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_submit_btn:
                String userName = loginUserName.getText().toString();
                String password = loginPassword.getText().toString();
                UserInteraction interaction = new UserInteraction(getApplicationContext());
                String nickName = interaction.login(userName,password);
                if (nickName != null){
                    Intent loginIntent = new Intent(LoginActivity.this,MainActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username",userName);
                    bundle.putString("password",password);
                    bundle.putString("nickname",nickName);
                    loginIntent.putExtra("userbundle",bundle);
                    loginIntent.putExtra("sign","fromlogin");
                    loginIntent.putExtra("hasdata",true);
                    startActivity(loginIntent);
                }
                break;
        }
    }
}
